package com.thinkgem.jeesite.modules.ship.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ship.entity.ShipSeaBook;
import com.thinkgem.jeesite.modules.ship.service.ShipSeaBookService;

/**
 * 出海预约信息Controller
 * @author lgx
 * @version 2016-04-17
 */
@Controller
@RequestMapping(value = "${adminPath}/ship/shipSeaBook")
public class ShipSeaBookController extends BaseController {

	@Autowired
	private ShipSeaBookService shipSeaBookService;
	
	@ModelAttribute
	public ShipSeaBook get(@RequestParam(required=false) String id) {
		ShipSeaBook entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = shipSeaBookService.get(id);
		}
		if (entity == null){
			entity = new ShipSeaBook();
		}
		return entity;
	}
	
	@RequiresPermissions("ship:shipSeaBook:view")
	@RequestMapping(value = {"shipSeaBookView"})
	public String shipSeaBookView(ShipSeaBook shipSeaBook, HttpServletRequest request, HttpServletResponse response, Model model,
			@RequestParam("year") String year,
			@RequestParam("month") String month,
			@RequestParam("weekDate") String weekDate,
			@RequestParam("dateTypeName") String dateTypeName
			) {
		Page<ShipSeaBook> page = shipSeaBookService.findViewPage(new Page<ShipSeaBook>(request, response), shipSeaBook); 
		model.addAttribute("page", page);
		return "modules/ship/shipSeaBookView";
	}
	
	@RequiresPermissions("ship:shipSeaBook:view")
	@RequestMapping(value = {"shipSeaBookList", ""})
	public String list(ShipSeaBook shipSeaBook, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ShipSeaBook> page = shipSeaBookService.findPage(new Page<ShipSeaBook>(request, response), shipSeaBook); 
		model.addAttribute("page", page);
		return "modules/ship/shipSeaBookList";
	}

	@RequiresPermissions("ship:shipSeaBook:view")
	@RequestMapping(value = "shipSeaBookForm")
	public String form(ShipSeaBook shipSeaBook, Model model) {
		model.addAttribute("shipSeaBook", shipSeaBook);
		return "modules/ship/shipSeaBookForm";
	}

	@RequiresPermissions("ship:shipSeaBook:edit")
	@RequestMapping(value = "save")
	public String save(ShipSeaBook shipSeaBook, Model model, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, shipSeaBook)){
			return form(shipSeaBook, model);
		}
		shipSeaBookService.save(shipSeaBook);
		renderStringByJbox(response, "提交成功");
		return null;
		
		//addMessage(redirectAttributes, "保存出海预约信息成功");
		//return "redirect:"+Global.getAdminPath()+"/ship/shipSeaBook/?repage";
	}
	
	@RequiresPermissions("ship:shipSeaBook:edit")
	@RequestMapping(value = "delete")
	public String delete(ShipSeaBook shipSeaBook, RedirectAttributes redirectAttributes) {
		shipSeaBookService.delete(shipSeaBook);
		addMessage(redirectAttributes, "删除出海预约信息成功");
		return "redirect:"+Global.getAdminPath()+"/ship/shipSeaBook/?repage";
	}

}